package admin

import (
	"chaowan/data_source"
	"chaowan/models"
	"github.com/gin-gonic/gin"
	"net/http"
)

type VipgiftsRBody struct {
	Data       []models.Vipgift `json:"data"`
	PageNo     int              `json:"pageNo"`
	PageSize   int              `json:"pageSize"`
	TotalCount int64            `json:"totalCount"`
	TotalPage  int64            `json:"totalPage"`
}

func Vipgifts(ctx *gin.Context) {
	request := NiudansBody{}
	ctx.ShouldBind(&request)
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Vipgift
	res := data_source.Db
	res.Model(&models.Vipgift{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := VipgiftsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type VipgiftBody struct {
	ID       int64   `json:"id" gorm:"id"`
	Dengji   int64   `json:"dengji" gorm:"dengji"`     // 等级
	Classify int64   `json:"classify" gorm:"classify"` // 1商品 2赏卷
	Price    float64 `json:"price" gorm:"price"`       // 消费满多少领取 0注册就领取
	Image    string  `json:"image" gorm:"image"`
	Name     string  `json:"name" gorm:"name"`
	Otherid  int64   `json:"otherid" gorm:"otherid"`
	Lingqu   int64   `json:"lingqu" gorm:"lingqu"` // 1未领取 2已领取
	Uid      int64   `json:"uid" gorm:"uid"`
	Goodname string  `json:"goodname" gorm:"goodname"` //赏卷or商品名称
}

func Vipgiftadd(ctx *gin.Context) {
	request := VipgiftBody{}
	ctx.ShouldBind(&request)
	dengji := request.Dengji
	classify := request.Classify
	otherid := request.Otherid
	goodname := request.Goodname
	name := request.Name
	image := request.Image
	price := request.Price
	gt := models.Vipgift{
		Name:     name,
		Image:    image,
		Dengji:   dengji,
		Classify: classify,
		Otherid:  otherid,
		Goodname: goodname,
		Price:    price,
	}
	data_source.Db.Create(&gt)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Vipgiftupdate(ctx *gin.Context) {
	request := VipgiftBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	dengji := request.Dengji
	classify := request.Classify
	otherid := request.Otherid
	goodname := request.Goodname
	name := request.Name
	image := request.Image
	price := request.Price
	gt := models.Vipgift{
		Name:     name,
		Image:    image,
		Dengji:   dengji,
		Classify: classify,
		Otherid:  otherid,
		Goodname: goodname,
		Price:    price,
	}
	data_source.Db.Where("id = ?", id).Updates(&gt)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Vipgiftdel(ctx *gin.Context) {
	request := NiudanBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	data_source.Db.Where("id = ?", id).Delete(&models.Vipgift{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
